package com.example.huawei.system.controller;

import com.example.huawei.system.entity.ReserveMaintain;
import com.example.huawei.system.entity.SysUser;
import com.example.huawei.system.result.Result;
import com.example.huawei.system.service.ReserveMaintainService;
import com.example.huawei.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ReserveMaintainService reserveMaintainService;

    @ApiOperation(value = "查询个人信息", notes = "查询个人信息")
    @GetMapping("/info")
    public Result<SysUser> edit(@RequestParam("id") String id) {
        SysUser user = sysUserService.getById(id);

        user.setMyReservation(reserveMaintainService.lambdaQuery().eq(ReserveMaintain::getUserId, id).count().intValue());
        return Result.ok(user);
    }

    @ApiOperation(value = "修改个人信息", notes = "修改个人信息")
    @PutMapping("/edit")
    public Result<SysUser> edit(@RequestBody SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return Result.ok(sysUserService.getById(sysUser.getId()));
    }
}
